package com.itheima.d2_xml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

/*
     目标：掌握使用Dom4j框架解析XML文件。
 */
public class Dom4JTest1 {
    public static void main(String[] args) throws Exception {
        //1、创建解析器对象
        SAXReader saxReader = new SAXReader();
        //2、通过解析器对象创建Document对象
        Document document = saxReader.read(new File("E:\\soft\\workspace\\javaAdvance\\Users.xml"));
        //3、获取根元素对象
        Element root = document.getRootElement();
        //4、获取根元素对象下的所有子元素对象
        List<Element> elements = root.elements();

        for (Element element : elements) {
            Attribute idAttr = element.attribute("id");
            String id = idAttr.getValue();

            Element nameElement = element.element("name");
            String name = nameElement.getText();

            Element ageElement = element.element("age");
            String age = ageElement.getText();

            Element genderElement = element.element("gender");
            String gender = genderElement.getText();

            Element emailElement = element.element("email");
            String email = emailElement.getText();

            Element birthdayElement = element.element("birthday");
            String birthday = birthdayElement.getText();

            Element addressElement = element.element("address");
            String address = addressElement.getText();

            Element phoneElement = element.element("phone");
            String phone = phoneElement.getText();
            System.out.print(id + "--" + name + "--" + age + "--" + gender + "--" + email + "--" + birthday + "--" + address + "--" + phone );

            Element hobbyElements = element.element("hobbys");
            List hobbyElement = hobbyElements.elements("hobby");
            for (Object o : hobbyElement) {
                Element element1 = (Element) o;
                String hobby = element1.getText();
                System.out.print("--"+hobby);


            }
            System.out.println();


        }

    }
}
